<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_FIELD_PATH."base_field_class.php");
    
	//
    // t_field_image
    //
	class t_field_image extends t_base_field
	{
        
        // property declaration
		// protected

        // property declaration
		// public
		// G
		const language_position_per_can_not_be_empty = 1;
        const language_position_per_invalid_value = 2;
        const language_position_per_test = 3;		
		const language_position_last = 4;
		// P
		const postfix_edit_image_tag_name = '_edit';
		const postfix_test_button_tag_name = '_test';
		const postfix_message_msg = '_msg';
		
		const proper_properties_position_per_NOT_EMPTY = 1;
		const proper_properties_position_per_label = 2; 
        const proper_properties_position_per_src_value = 3;
		const proper_properties_position_per_alt_value = 4;
		const proper_properties_position_per_to_edit = 5; 
        const proper_properties_position_last = 6;
        // method declaration
    	// protected

		
        // A
        protected function adds_check_value_method()
        {
            global $g_javascript_namespace;
         	$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
				get_class($this),"check_value","a_name","
    var local_name = a_name;
	if (a_name == undefined)
		local_name = this.name; 
    var local_img = this.html_tag(local_name);
	var local_edit = this.html_tag(local_name+'".self::postfix_edit_image_tag_name."');
	if (local_edit == undefined)
		return true;
    local_img.src = local_edit.value.trim();
	var local_value = local_edit.value.trim();          
    var local_msg_tag = this.html_tag(local_name+'".self::postfix_message_msg."');
    var local_msg = '';
    var local_return = ((local_value != '') || (!this.can_not_be_empty));
    if (!local_return)
    {
		local_msg = this.can_not_be_empty_msg;
    }
	else 
	{
		if (local_value != '')
		{
			local_return = local_value.validate_link(local_value);
			if (! local_return)
			{
				local_msg = this.invalid_value_msg;
			}    
		}
    }    
    if (!local_return)
        local_msg_tag.innerHTML = local_msg;
    else
        local_msg_tag.innerHTML = '';    
    
	return local_return;
    "); 
$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
			get_class($this),"edit_row_activation","a_tag_value_name,a_tag_value_edit_name,a_active,a_copy_value","
	var local_value_style = '';
	var local_edit_value_style = 'none';
	if (a_active == true)
	{
		local_value_style = 'none';
		local_edit_value_style = '';
	}
	var local_value = set_tag_style_property(['display'],[local_value_style],a_tag_value_name,false);
	set_tag_style_property(['display'],[local_edit_value_style],a_tag_value_edit_name+'".self::postfix_message_msg."',false);
	set_tag_style_property(['display'],[local_edit_value_style],a_tag_value_edit_name,false);
	var local_edit_value = set_tag_style_property(['display'],[local_edit_value_style],a_tag_value_edit_name+'".self::postfix_edit_image_tag_name."',false);
	if (local_edit_value == undefined)
		return true;
	set_tag_style_property(['display'],[local_edit_value_style],a_tag_value_edit_name+'".self::postfix_test_button_tag_name."',false);
	
	if (a_copy_value)
	{
		if (a_active == true)
		{
			set_tag_property(['value'],[local_value.src],local_edit_value,false);
		} 	
		else
		{
			set_tag_property(['src'],[local_edit_value.value],local_value,false);
		} 

	}		
	");	

   /*	$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
		get_class($this),"get_prepared_value","a_name","
	var local_name = a_name;
	if (a_name == undefined)
		local_name = this.name;
	var local_link = this.html_tag(local_name);       
    return '[\"'+local_link.innerHTML + '\",\"' + local_link.href+'\"]';
    ");	*/
    
        }
        // G
		protected function get_field_event_script($a_event_type,$a_argouments)
        {			
			$local_return = '';
			return $local_return;
		}
static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = 'TRUE';
				break;                
			}*/
			return $local_return;
		}
		protected function get_proper_properties_position_last()      
		{
            return self::proper_properties_position_last;
        }
		
        // method declaration
    	// public
        public function __construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str)
		{
            parent::__construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str);
            
			
		}

		// C
		
        public function create_tags_structure()
        {
            global $g_javascript_namespace;
            //echo __METHOD__."<BR>";
            //echo "this->proper_properties_array[self::proper_properties_position_per_NOT_EMPTY]: '".$this->proper_properties_array[self::proper_properties_position_per_NOT_EMPTY]."'<br>";
            if (($this->proper_properties_array[self::proper_properties_position_per_NOT_EMPTY] == 1) ||
                (strtoupper($this->proper_properties_array[self::proper_properties_position_per_NOT_EMPTY]) == "TRUE"))
                $local_can_not_be_empty = "true";
            else
                $local_can_not_be_empty = "false";
           // echo "local_can_not_be_empty: $local_can_not_be_empty<br>";
			$local_can_not_be_empty_msg = addslashes($this->lang[self::language_position_per_can_not_be_empty]);	
            $local_invalid_value_msg = addslashes($this->lang[self::language_position_per_invalid_value]);	
            parent::create_tags_structure(
				// ARGUMENTS DECLARATIONS
                "a_can_not_be_empty,a_can_not_be_empty_msg,a_invalid_value_msg",
                // ARGUMENTS VALUES
                "$local_can_not_be_empty,'$local_can_not_be_empty_msg','$local_invalid_value_msg'");
			// proprierties

			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'can_not_be_empty',"a_can_not_be_empty",false);
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'can_not_be_empty_msg',"a_can_not_be_empty_msg",false);	
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'invalid_value_msg',"a_invalid_value_msg",false);                
            // STYLE
			// tags    
            $local_name = $this->common_properties_array[t_base_field::common_properties_position_per_field_name];
            $local_label = $this->proper_properties_array[self::proper_properties_position_per_label];
            if (trim($local_label) != '')
            {
                $local_current_tag = parent::add_tag(t_base_tag_type::btt_label,$local_name . "_label");
                $this->label_tag = $local_current_tag;
				$local_current_tag->set_text_to_include($local_label);
            }    
            // IMAGE TAG
			if (($this->proper_properties_array[self::proper_properties_position_per_to_edit] == 1) ||
                (strtoupper($this->proper_properties_array[self::proper_properties_position_per_to_edit]) == "TRUE"))			
			{	
				$this->edit_image_tag = parent::add_tag(t_base_tag_type::btt_input,
					$local_name. self::postfix_edit_image_tag_name);
				$this->edit_image_tag->add_property('value',$this->proper_properties_array[self::proper_properties_position_per_src_value]);	
				//	
				$local_current_tag = parent::add_tag(t_base_tag_type::btt_image,
					$local_name);
				$this->test_btn_tag = parent::add_tag(t_base_tag_type::btt_button,
					$local_name.self::postfix_test_button_tag_name);
				$this->test_btn_tag->set_text_to_include(
					$this->lang[self::language_position_per_test]);	
					
				$this->test_btn_tag->add_property("onclick",t_cuteengine::javascript_instance_name.".".$this->name.".do_test(event)");
					
			}		
			else
			{
				$local_current_tag = parent::add_tag(t_base_tag_type::btt_image,
					$local_name);
				$local_current_tag->add_property('src',$this->proper_properties_array[self::proper_properties_position_per_src_value]);		
				$local_current_tag->add_property('alt',$this->proper_properties_array[self::proper_properties_position_per_alt_value]);

			}
			
			$this->image_tag = $local_current_tag;
			$this->main_tag = $local_current_tag;			
            $this->default_start_tag = $this->main_tag;
            $this->default_result_tag = $this->main_tag;
			// message image tag
			$local_current_tag = parent::add_tag(t_base_tag_type::btt_label,$local_name . self::postfix_message_msg);
            $this->message_tag = $local_current_tag;
			$this->message_tag->class_name = t_base_field::error_msg_class_name;	

            // method
            $this->adds_check_value_method();
    
        }
// this method permit to add method or function used to manage data in javascript script
		public function create_javascript_initialization_component()
		{
			global $g_javascript_namespace;
			// I
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
				get_class($this),'do_test','event',"
	var local_target = event.target ? event.target : event.srcElement;
	var local_img = local_target.previousSibling;
	var local_input = local_img.previousSibling;
	if (local_input.tagName != 'INPUT')
		local_input = local_input.previousSibling;
	if (this.check_value(local_img.id))
	{
		local_img.src = local_input.value;
		local_img.style.display = '';
	}
	"); 
		}
        // G
		public function get_included_in_html_structure($a_name,$a_display,$a_key_tag_name)
		{
			parent::get_included_in_html_structure($a_name);
			if ($a_display)
			{
				$this->image_tag->add_style_property('display','');
				if (isset($this->test_btn_tag))
				{
					$this->message_tag->add_style_property('display','');
					$this->test_btn_tag->add_style_property('display','');
					$this->edit_image_tag->add_style_property('display','');
				}	
				
			}
			else
			{
				$this->image_tag->add_style_property('display','none');
				
				if (isset($this->test_btn_tag))
				{
					$this->message_tag->add_style_property('display','none');
					$this->test_btn_tag->add_style_property('display','none');
					$this->edit_image_tag->add_style_property('display','none');
				}	

			}	
	
			$this->key_tags_list[$a_name] = $a_key_tag_name;
			//echo "a_name: $a_name - a_key_tag_name: $a_key_tag_name<br>";
			$local_label_script = '';
			if (isset($this->label_tag))
				$local_label_script = $this->label_tag->generate_html_code_with_new_name($a_name.t_field_text::postfix_label_tag_name);
			//	
			if (($this->proper_properties_array[self::proper_properties_position_per_to_edit] == 1) ||
                (strtoupper($this->proper_properties_array[self::proper_properties_position_per_to_edit]) == "TRUE"))			
			{
				//	
				return $local_label_script .
					($this->edit_image_tag->generate_html_code_with_new_name($a_name.self::postfix_edit_image_tag_name) .
					$this->image_tag->generate_html_code_with_new_name($a_name) .
					$this->test_btn_tag->generate_html_code_with_new_name($a_name.self::postfix_test_button_tag_name) .
					$this->message_tag->generate_html_code_with_new_name($a_name.t_field_text::postfix_message_tag_name));
			}
			else
			{
				return $local_label_script .
					($this->image_tag->generate_html_code_with_new_name($a_name) .
					$this->message_tag->generate_html_code_with_new_name($a_name.t_field_text::postfix_message_tag_name));
			}
			
		}
		public function get_included_in_html_structure_no_edit_mode($a_id,$a_value,$a_label_value)
		{
			$this->image_tag->add_style_property('display','');
			$this->image_tag->add_property('src',$a_value);		
			$this->image_tag->add_property('alt',$this->proper_properties_array[self::proper_properties_position_per_alt_value]);
			return $this->image_tag->generate_html_code_with_new_name($a_id);
		}
		
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
		public function get_result_action_script($a_event_type,$parent_event_type,
            $a_parameter,$a_content_loader_result)
		{
            return '';
        }		
		
// I
		public function initialize_field_event($a_event_type)
        {			
			
		}
        
       

		

    }
?>    